package com.wushi.lenovo.asia5b.app_ui.orderdetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.base.BaseAppCompatActivity;
import com.mvp.images.WImageLoader;
import com.mvp.utils.ResString;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.bean.PriceDetailType;
import com.wushi.lenovo.asia5b.app_model.good.goodbean.GoodsModelIml;
import com.wushi.lenovo.asia5b.app_ui.h5.GoodsDetailsActivity;
import com.wushi.lenovo.asia5b.app_widget.mingan.MinganImageView;

import java.io.Serializable;

/**
 * Created by zhangyuncai on 2020/3/5.
 */
public class PriceDetailActivity extends BaseAppCompatActivity {

    private PriceDetailType type;
    private GoodsModelIml model;

    public static void startPriceDetailActivity(Context context, Serializable serializable, Serializable type) {
        Intent intent = new Intent(context, PriceDetailActivity.class);
        intent.putExtra("data", serializable);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        type = (PriceDetailType) getIntent().getSerializableExtra("type");
        model = (GoodsModelIml) getIntent().getSerializableExtra("data");
        if (type == PriceDetailType.ordercheckdaigou) {
            return R.layout.activity_pricedetail_ordercheckdaigou;
        } else if (type == PriceDetailType.ordercheckpi) {
            return R.layout.activity_pricedetail_ordercheckpi;
        } else if (type == PriceDetailType.orderpack) {
            return R.layout.activity_pricedetail_orderpack;
        }
        return R.layout.activity_pricedetail_ordercheckdaigou;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initEvent();
    }

    @Override
    protected void initView() {
        if (type == PriceDetailType.ordercheckdaigou) {
            setOrderCheckDaigouView();
        } else if (type == PriceDetailType.ordercheckpi) {
            setOrderCheckPiView();
        } else if (type == PriceDetailType.orderpack) {
            setOrderPackView();
        }
    }

    protected void setOrderCheckDaigouView() {
        // 图片
        MinganImageView mingan_pricedetail = findViewById(R.id.mingan_pricedetail);
        mingan_pricedetail.setMinganTextVisibility(false);
        WImageLoader.displayImage(mActivity, model.goodsImg(), mingan_pricedetail.getImageView());
        mingan_pricedetail.setOnClickListener(v -> {
            GoodsDetailsActivity.startGoodsDetailsActivity(mActivity, model.goodsId());
        });

        //商品名
        TextView tv_pricedetail_goodname = findViewById(R.id.tv_pricedetail_goodname);
        tv_pricedetail_goodname.setText(model.goodsName());
        tv_pricedetail_goodname.setOnClickListener(v -> {
            GoodsDetailsActivity.startGoodsDetailsActivity(mActivity, model.goodsId());
        });


        //详细
        TextView tv_pricedetail_attr = findViewById(R.id.tv_pricedetail_attr);
        tv_pricedetail_attr.setText(String.format(ResString.get(R.string.orderitem_details), model.goodsAttr()));

        //备注
        LinearLayout ll_pricedetail_remark_vessel = findViewById(R.id.ll_pricedetail_remark_vessel);
        ll_pricedetail_remark_vessel.setVisibility(TextUtils.isEmpty(model.getRemark()) ? View.GONE : View.VISIBLE);
        TextView tv_pricedetail_remark = findViewById(R.id.tv_pricedetail_remark);
        tv_pricedetail_remark.setText(model.getRemark());

        //单价
        TextView tv_pricedetail_productunitprice = findViewById(R.id.tv_pricedetail_productunitprice);
        tv_pricedetail_productunitprice.setText(model.goodsUnitPrice());

        //数量
        TextView tv_pricedetail_productpricecnt = findViewById(R.id.tv_pricedetail_productpricecnt);
        tv_pricedetail_productpricecnt.setText(String.format("Quantity x %d", model.goodsQuantity()));

        //中国物流费用
        TextView tv_pricedetail_chinafeeprice = findViewById(R.id.tv_pricedetail_chinafeeprice);
        tv_pricedetail_chinafeeprice.setText(model.chinaDeliveryFee());

        //总价
        TextView tv_pricedetail_totalprice = findViewById(R.id.tv_pricedetail_totalprice);
        tv_pricedetail_totalprice.setText(model.zongjiPrice());
    }

    protected void setOrderCheckPiView() {
        //包裹号
        TextView tv_pricedetail_parcelno = findViewById(R.id.tv_pricedetail_parcelno);
        tv_pricedetail_parcelno.setText(model.parcelno() + "");

        // 图片
        MinganImageView mingan_pricedetail = findViewById(R.id.mingan_pricedetail);
        mingan_pricedetail.setMinganTextVisibility(false);
        WImageLoader.displayImage(mActivity, model.goodsImg(), mingan_pricedetail.getImageView());
        mingan_pricedetail.setOnClickListener(v -> {
            GoodsDetailsActivity.startGoodsDetailsActivity(mActivity, model.goodsId());
        });

        //商品名
        TextView tv_pricedetail_goodname = findViewById(R.id.tv_pricedetail_goodname);
        tv_pricedetail_goodname.setText(model.goodsName());
        tv_pricedetail_goodname.setOnClickListener(v -> {
            GoodsDetailsActivity.startGoodsDetailsActivity(mActivity, model.goodsId());
        });

        //详细
        TextView tv_pricedetail_attr = findViewById(R.id.tv_pricedetail_attr);
        tv_pricedetail_attr.setText(String.format(ResString.get(R.string.orderitem_details), model.goodsAttr()));

        //备注
        LinearLayout ll_pricedetail_remark_vessel = findViewById(R.id.ll_pricedetail_remark_vessel);
        ll_pricedetail_remark_vessel.setVisibility(TextUtils.isEmpty(model.getRemark()) ? View.GONE : View.VISIBLE);
        TextView tv_pricedetail_remark = findViewById(R.id.tv_pricedetail_remark);
        tv_pricedetail_remark.setText(model.getRemark());

        //单价
        TextView tv_pricedetail_productunitprice = findViewById(R.id.tv_pricedetail_productunitprice);
        tv_pricedetail_productunitprice.setText(model.goodsUnitPrice());


        //总立方米
        TextView tv_pricedetail_m3weight = findViewById(R.id.tv_pricedetail_m3weight);
        tv_pricedetail_m3weight.setText(model.goodsTotalM3Weight());

        //数量
        TextView tv_pricedetail_productpricecnt = findViewById(R.id.tv_pricedetail_productpricecnt);
        tv_pricedetail_productpricecnt.setText(String.format("Quantity x %d", model.goodsQuantity()));

        //总价
        TextView tv_pricedetail_totalprice = findViewById(R.id.tv_pricedetail_totalprice);
        tv_pricedetail_totalprice.setText(model.zongjiPrice());
    }

    protected void setOrderPackView() {
        //包裹号
        TextView tv_pricedetail_parcelno = findViewById(R.id.tv_pricedetail_parcelno);
        tv_pricedetail_parcelno.setText(model.parcelno() + "");

        // 图片
        MinganImageView mingan_pricedetail = findViewById(R.id.mingan_pricedetail);
        mingan_pricedetail.setMinganTextVisibility(false);
        WImageLoader.displayImage(mActivity, model.goodsImg(), mingan_pricedetail.getImageView());
        mingan_pricedetail.setOnClickListener(v -> {
            GoodsDetailsActivity.startGoodsDetailsActivity(mActivity, model.goodsId());
        });

        //商品名
        TextView tv_pricedetail_goodname = findViewById(R.id.tv_pricedetail_goodname);
        tv_pricedetail_goodname.setText(model.goodsName());
        tv_pricedetail_goodname.setOnClickListener(v -> {
            GoodsDetailsActivity.startGoodsDetailsActivity(mActivity, model.goodsId());
        });

        //详细
        TextView tv_pricedetail_attr = findViewById(R.id.tv_pricedetail_attr);
        tv_pricedetail_attr.setText(String.format(ResString.get(R.string.orderitem_details), model.goodsAttr()));

        //备注
        LinearLayout ll_pricedetail_remark_vessel = findViewById(R.id.ll_pricedetail_remark_vessel);
        ll_pricedetail_remark_vessel.setVisibility(TextUtils.isEmpty(model.getRemark()) ? View.GONE : View.VISIBLE);
        TextView tv_pricedetail_remark = findViewById(R.id.tv_pricedetail_remark);
        tv_pricedetail_remark.setText(model.getRemark());

        /*
        Total Actual Weight = weight
        Total Vol. Weight (i) = volume_weight_remark
        Total Vol. Weight = volume_weight
        Total M3 = m3_weight」
         */
        //总实际重量
        TextView tv_pricedetail_totalactualweight = findViewById(R.id.tv_pricedetail_totalactualweight);
        tv_pricedetail_totalactualweight.setText(model.goodsTotalWeight());

        //总体积重量
        TextView tv_pricedetail_totalvolweight = findViewById(R.id.tv_pricedetail_totalvolweight);
        tv_pricedetail_totalvolweight.setText(model.goodsTotalVolWeight());
        //提示
        ImageView iv_pricedetail_volweight_notice = findViewById(R.id.iv_pricedetail_volweight_notice);
        iv_pricedetail_volweight_notice.setOnClickListener(v -> {
            ToastUtil.showToast(getString(R.string.pricedetail_totalvolweight_notice));
        });


        //总立方米
        TextView tv_pricedetail_m3weight = findViewById(R.id.tv_pricedetail_m3weight);
        tv_pricedetail_m3weight.setText(model.goodsTotalM3Weight());

        //单价
        TextView tv_pricedetail_productunitprice = findViewById(R.id.tv_pricedetail_productunitprice);
        tv_pricedetail_productunitprice.setText(model.goodsUnitPrice());

        //数量
        TextView tv_pricedetail_productpricecnt = findViewById(R.id.tv_pricedetail_productpricecnt);
        tv_pricedetail_productpricecnt.setText(String.format("Quantity x %d", model.goodsQuantity()));

        //中国物流费用
        TextView tv_pricedetail_chinafeeprice = findViewById(R.id.tv_pricedetail_chinafeeprice);
        tv_pricedetail_chinafeeprice.setText(model.chinaDeliveryFee());

        //补差价
        LinearLayout ll_pricedetail_difftotalprice = findViewById(R.id.ll_pricedetail_difftotalprice);
        TextView tv_pricedetail_difftotalprice = findViewById(R.id.tv_pricedetail_difftotalprice);
        tv_pricedetail_difftotalprice.setText(model.totalDiffPrice());
        ll_pricedetail_difftotalprice.setVisibility(TextUtils.isEmpty(model.totalDiffPrice()) ? View.GONE : View.VISIBLE);

        //总价
        TextView tv_pricedetail_totalprice = findViewById(R.id.tv_pricedetail_totalprice);
        tv_pricedetail_totalprice.setText(model.zongjiPrice());

        //储存天数
        LinearLayout ll_pricedetail_storage = findViewById(R.id.ll_pricedetail_storage);
        TextView tv_pricedetail_storage = findViewById(R.id.tv_pricedetail_storage);
        ImageView iv_pricedetail_storage = findViewById(R.id.iv_pricedetail_storage);

        int storage_day_numF = 0;
        try {
            storage_day_numF = Integer.parseInt(model.getStorage_day_num());
        } catch (Exception e) {
            e.printStackTrace();
            storage_day_numF = 0;
        }
        ll_pricedetail_storage.setVisibility(TextUtils.isEmpty(model.getStorage_day_num()) ? View.GONE : View.VISIBLE);
        if (!TextUtils.isEmpty(model.getStorage_day_num())) {

            if (storage_day_numF > 0) {//存储已过期
                tv_pricedetail_storage.setText(String.format(getString(R.string.storage_remaining), storage_day_numF));
            } else {//存储未过期
                tv_pricedetail_storage.setText(String.format(getString(R.string.storage_exceeded), Math.abs(storage_day_numF)));
            }
            int finalStorage_day_numF = storage_day_numF;
            iv_pricedetail_storage.setOnClickListener(v -> {
                String toastText = String.format(getString(R.string.storage_note), finalStorage_day_numF, model.getStorage_total_days());
                ToastUtil.showToast(toastText);
            });
        }

    }
}
